package com.star.controller.business;

import com.star.constant.JwtClaimsConstant;
import com.star.dto.BusinessDTO;
import com.star.dto.BusinessLoginDTO;
import com.star.dto.PasswordEditDTO;
import com.star.entity.Business;
import com.star.properties.JwtProperties;
import com.star.result.Result;
import com.star.service.BusinessService;
import com.star.utils.JwtUtil;
import com.star.vo.business.BusinessLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 员工管理
 */
@RestController("businessController")
@RequestMapping("/business")
@Slf4j
// Swagger接口文档
@Api(tags = "商家端管理接口")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录
     *
     * @param businessLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("商家登录")
    public Result<BusinessLoginVO> login(@RequestBody BusinessLoginDTO businessLoginDTO) {
        log.info("商家登录：{}", businessLoginDTO);

        Business business = businessService.login(businessLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, business.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getBusinessSecretKey(),
                jwtProperties.getBusinessTtl(),
                claims);

        // 将员工信息缓存到redis中
        String empToken = "emp_" + business.getId();
        redisTemplate.opsForValue().set(empToken,token,1,TimeUnit.DAYS);

        BusinessLoginVO businessLoginVO = BusinessLoginVO.builder()
                .id(business.getId())
                .userName(business.getUsername())
                .name(business.getName())
                .avatar(business.getAvatar())
                .token(token)
                .build();

        return Result.success(businessLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工登出")
    public Result<String> logout() {

        return Result.success();
    }
    /**
     * 根据id查询商家信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商家信息")
    public Result<Business> getById(@PathVariable Long id){
        log.info("商家id{}", id);
        Business business = businessService.getById(id);
        return Result.success(business);
    }

    /**
     * 更新商家信息
     * @param businessDTO body参数
     * @return
     */
    @PutMapping
    @ApiOperation("更新商家信息")
    public Result<String> update(@RequestBody BusinessDTO businessDTO){
        log.info("更新商家信息{}", businessDTO);
        businessService.update(businessDTO);
        return Result.success();
    }
    /**
     * 修改用户头像
     * @param avatar
     * @return
     */
    @PutMapping("/avatar")
    public Result<String> updateAvatar(@RequestParam Long id, @RequestParam String avatar){
        if(avatar == null || avatar.isEmpty()){
            return Result.error("头像不能为空");
        }
        businessService.updateAvatar(id, avatar);
        return Result.success();
    }
    /**
     * 更新账户密码
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("更新账户密码")
    public Result<String> updatePwd(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改密码{}", passwordEditDTO);
        businessService.updatePwd(passwordEditDTO);
        String empToken = "emp_" + passwordEditDTO.getId();
        redisTemplate.delete(empToken);
        return Result.success();
    }
}
